package com.zhs.gccs.view;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zhs.gccs.R;
import com.zhs.gccs.base.BaseActivity;
import com.zhs.gccs.model.PointInfo;
import com.zhs.gccs.dao.PointInfoDao;
import com.zhs.gccs.model.PointBean;
import com.zhs.gccs.adapter.MenuAdapter;

import java.util.ArrayList;
import java.util.List;

public class ProjectItemList extends LinearLayout {
    public ProjectItemList(Context context) {
        super(context);
        init(context);
    }

    public ProjectItemList(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ProjectItemList(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private TextView item_project_detail_title;
    private TextView item_project_list_item_tv2;
    private TextView item_project_list_item_tv3;
    private TextView item_project_list_item_tv4;
    private TextView item_project_list_item_tv5;
    private TextView item_project_list_item_tv6;
    private TextView items_inside;
    private TextView items_outside;
    private ImageView items_inside_icon;
    private ImageView items_outside_icon;
    private RecyclerView item_project_list_item_rv;
    private Context context;
    private ItemTaskDetailListener itemTaskDetailListener;
    private List<PointBean> list = new ArrayList<>();
    private MenuAdapter<PointBean> adapter;
    private PointInfoDao pointInfoDao;

    private boolean isInside = true;

    List<PointBean> listInside = new ArrayList<>();
    List<PointBean> listOutside = new ArrayList<>();


    public void setItemTaskDetailListener(ItemTaskDetailListener itemTaskDetailListener) {
        this.itemTaskDetailListener = itemTaskDetailListener;
    }

    private void init(Context context){
        pointInfoDao = new PointInfoDao();
        this.context = context;
        View view = LayoutInflater.from(context).inflate(R.layout.item_task_detail, null);
        addView(view);

        LinearLayout.LayoutParams params = (LayoutParams) view.getLayoutParams();
        params.width = (int) (BaseActivity.displayWidth * 0.41);
        params.height = BaseActivity.MATCH_PARENT;
        params.rightMargin = (int) (10 * BaseActivity.density);
        params.topMargin = (int) (10 * BaseActivity.density);
        params.bottomMargin = (int) (10 * BaseActivity.density);
        view.setLayoutParams(params);

        ImageView close = view.findViewById(R.id.item_project_detail_close);
        close.setOnClickListener(view1 -> {
            closeListener.onClose(1);
//            setVisibility(GONE);
        });

        //标题 项目名称
        item_project_detail_title = view.findViewById(R.id.item_project_detail_title);

        //项目编号
        item_project_list_item_tv2 = view.findViewById(R.id.item_task_list_item_tv2);
        //建设面积
        item_project_list_item_tv3 = view.findViewById(R.id.item_task_list_item_tv3);
        //建成面积
        item_project_list_item_tv4 = view.findViewById(R.id.item_task_list_item_tv4);
        //高标准年度
        item_project_list_item_tv5 = view.findViewById(R.id.item_task_list_item_tv5);
        //项目主管部门
        item_project_list_item_tv6 = view.findViewById(R.id.item_task_list_item_tv6);

        //范围内、范围外
        items_inside = view.findViewById(R.id.items_inside);
        items_outside = view.findViewById(R.id.items_outside);
        items_inside_icon = view.findViewById(R.id.items_inside_icon);
        items_outside_icon = view.findViewById(R.id.items_outside_icon);

        items_inside.setOnClickListener(v->{
            isInside = true;
            items_inside.setTextColor(getResources().getColor(android.R.color.white));
            items_inside_icon.setVisibility(VISIBLE);
            items_outside.setTextColor(getResources().getColor(R.color.custom_white));
            items_outside_icon.setVisibility(INVISIBLE);
            list.clear();
            list.addAll(listInside);
            adapter.notifyDataSetChanged();
            sideChangeStateListener.onStateChange(0);
        });
        items_outside.setOnClickListener(v->{
            isInside = false;
            items_outside.setTextColor(getResources().getColor(android.R.color.white));
            items_outside_icon.setVisibility(VISIBLE);
            items_inside.setTextColor(getResources().getColor(R.color.custom_white));
            items_inside_icon.setVisibility(INVISIBLE);
            list.clear();
            list.addAll(listOutside);
            adapter.notifyDataSetChanged();
            sideChangeStateListener.onStateChange(1);
        });

        //项目下设备列表

        item_project_list_item_rv = view.findViewById(R.id.item_project_list_item_rv);
        item_project_list_item_rv.setLayoutManager(new LinearLayoutManager(context));

        adapter = new MenuAdapter<PointBean>(list) {
            @Override
            public int getLayoutId(int viewType) {
//                if(viewType == -1){
//                    return R.layout.item_task_detail_item_1;
//                }
                return R.layout.item_task_detail_item;
            }

            @Override
            public int getItemViewType(int position) {
                if(position == 0){
                    return -1;
                }

                return super.getItemViewType(position);
            }

            @Override
            public void convert(VH holder, PointBean data, int position) {
                TextView item_project_item_name = holder.getView(R.id.item_project_item_name);
                TextView check_state = holder.getView(R.id.check_state);
                LinearLayout project_item = holder.getView(R.id.project_item);
                ImageView right_icon = holder.getView(R.id.right_icon);
                project_item.setOnClickListener(v->{
                    if(isInside){
                        itemTaskDetailListener.onItemDetail(data.getPointId());
                    }

                });
                if(isInside){
                    right_icon.setVisibility(VISIBLE);
                }else {
                    right_icon.setVisibility(INVISIBLE);
                }
                item_project_item_name.setText(data.getPointName());//project_item_name'
                item_project_item_name.setText(
                        String.format(
                                context.getString(R.string.project_item_name),data.getPointName()
                        )

                );

                if(data.getCheckState() == 0){
                    check_state.setBackgroundResource(R.mipmap.bg_gray);
                    check_state.setTextColor(getResources().getColor(android.R.color.darker_gray));
                    check_state.setText("未核查");
                }else{
                    check_state.setBackgroundResource(R.mipmap.bg_green);
                    check_state.setTextColor(getResources().getColor(android.R.color.white));
                    check_state.setText("已核查");
                }
            }
        };
        item_project_list_item_rv.setAdapter(adapter);
        item_project_list_item_rv.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.bottom = (int) (BaseActivity.density * 10);
            }
        });

    }

    public void items_insideClick(){
        items_inside.performLongClick();
    }

    public void setListInside(List<PointBean> listInside){


        this.listInside = listInside;
    }
    public void setListOutside(List<PointBean> listOutside){
        this.listOutside = listOutside;
    }
    public void changeSide(List<PointBean> listInside,List<PointBean> listOutside,int state){
        items_outside.setText("范围外（"+listOutside.size()+")");
        items_inside.setText("范围内（"+listInside.size()+"）");
        if(state == 0){
            list.clear();
            list.addAll(listInside);
            adapter.notifyDataSetChanged();
        }else{
            list.clear();
            list.addAll(listOutside);
            adapter.notifyDataSetChanged();
        }
    }
    public void changeSide(List<PointBean> listInside,List<PointBean> listOutside){
        items_outside.setText("范围外（"+listOutside.size()+")");
        items_inside.setText("范围内（"+listInside.size()+"）");

    }
    public void resetSide(int state){
        listInside.clear();
        listOutside.clear();
        items_outside.setText("范围外（0)");
        items_inside.setText("范围内（0）");
        updateList(state);
    }

    private SideChangeStateListener sideChangeStateListener;
    public void setSideChangeStateListener(SideChangeStateListener sideChangeStateListener){
        this.sideChangeStateListener = sideChangeStateListener;
    }
    public interface SideChangeStateListener{
        void onStateChange(int state);
    }
    public Drawable getMyDrawable(@DrawableRes int id) {
        return ContextCompat.getDrawable(context, id);
    }

//    public void setList(List<PointBean> list){
//        this.list.clear();
//        this.list.addAll(list);
//        adapter.notifyDataSetChanged();
//    }

    public void updateList(int state){
        list.clear();
        if(state == 0){
            list.addAll(listInside);
        }else{
            list.addAll(listOutside);
        }

        adapter.notifyDataSetChanged();
    }
    public void setTaskPid(String pid){
        List<PointInfo> list = pointInfoDao.findPointInfoListAll(pid);
        List<PointBean> pointBeans = new ArrayList<>();
        if(list == null)
            return;
        for (PointInfo pointInfo : list){
            PointBean pointBean = new PointBean();
            pointBean.setId((pointInfo.getId()-1)+"");
            pointBean.setpId(pointInfo.getpId());
            pointBean.setPointId(pointInfo.getPointId());
            pointBean.setReadme(pointInfo.getReadme());
            pointBean.setPointName(pointInfo.getPointName());
            pointBean.setAttribute(pointInfo.getAttribute());

            pointBean.setImagePath1(pointInfo.getImagePath1());
            pointBean.setImagePath2(pointInfo.getImagePath2());
            pointBean.setImagePath3(pointInfo.getImagePath3());

            pointBean.setIsExist(pointInfo.getIsExist());
            pointBean.setCheckState(pointInfo.getIsCheck());

            pointBeans.add(pointBean);
        }

//        this.list.clear();
//        this.list.addAll(pointBeans);
//        adapter.notifyDataSetChanged();
    }

    public void setTaskName(String taskName) {
        item_project_detail_title.setText(taskName);
    }

    public void setTaskCode(String taskCode) {
        item_project_list_item_tv2.setText(taskCode);
    }

    public void setArea1(String area1) {
        item_project_list_item_tv3.setText(area1);
    }

    public void setArea2(String area2) {
        item_project_list_item_tv4.setText(area2);
    }

    public void setYear(String year) {
        item_project_list_item_tv5.setText(year);
    }

    public void setDepartment(String department) {
        item_project_list_item_tv6.setText(department);
    }


    public void setImagePath(int position, String imagePath) {
        PointBean pointBean = list.get(position);
        if(pointBean != null){
            String imagePath1 = pointBean.getImagePath1();
            String imagePath2 = pointBean.getImagePath2();
            String imagePath3 = pointBean.getImagePath3();

            if(TextUtils.isEmpty(imagePath1)){
                pointBean.setImagePath1(imagePath);
            }else if (TextUtils.isEmpty(imagePath2)){
                pointBean.setImagePath2(imagePath);
            }else if (TextUtils.isEmpty(imagePath3)){
                pointBean.setImagePath3(imagePath);
            }

            adapter.notifyItemChanged(position);

        }
    }

    public interface ItemTaskDetailListener {
//        void takePhoto(int position);
        void onItemDetail(String pointId);
    }

    private ItemPointDetail.CloseListener closeListener;
    public void setCloseListener(ItemPointDetail.CloseListener closeListener){
        this.closeListener = closeListener;
    }

    public interface CloseListener{
        void onClose(int state);
    }
}
